package com.jeasonzhao.report.decorator;

import com.jeasonzhao.report.codes.ReportHeaderModes;
import com.jeasonzhao.report.dataset.DataSet;
import com.jeasonzhao.report.dataset.RowInfoCollection;
import com.jeasonzhao.report.engine.irp.IDataSetDecorator;
import com.jeasonzhao.report.exceptions.DecoratorException;
import com.jeasonzhao.report.model.Report;

public class CustomerHeaderDecorator implements IDataSetDecorator
{
//    private static final Log log = LogFactory.getLog(CustomerHeaderDecorator.class);
    private static volatile CustomerHeaderDecorator m_instance = null;
    public static CustomerHeaderDecorator getInstance()
    {
        if(null == m_instance)
        {
            synchronized(CustomerHeaderDecorator.class)
            {
                m_instance = new CustomerHeaderDecorator();
            }
        }
        return m_instance;
    }

    private CustomerHeaderDecorator()
    {
        super();
    }

    public DataSet decorate(Report report,DataSet dataset)
        throws DecoratorException
    {
        if(null == report || dataset == null || report.getHeaderMode().isDefault())
        {
            return dataset;
        }
        /**
         * @todo 此处强制将自定义表头的东东设置成不能自定义
         */
        report.setCanBeCustomized(false);
        if(null == report.getUserDefinedTableHeader())
        {
            dataset.setHeader(null);
            return dataset;
        }
        RowInfoCollection cHeader = report.getUserDefinedTableHeader();
        /**
         * @todo 此处可以加入自定义表头中的计算表达式的计算，
         */
        RowInfoCollection header = dataset.getHeader();
        if(report.getHeaderMode().equals(ReportHeaderModes.CustomAfterDefault))
        {
            for(int n = 0;null != header && n < header.size();n++)
            {
                cHeader.insertElementAt(header.get(n),n);
            }
        }
        else if(report.getHeaderMode().equals(ReportHeaderModes.CustomBeforeDefault))
        {
            for(int n = 0;null != header && n < header.size();n++)
            {
                cHeader.addElement(header.get(n));
            }
        }
        dataset.setHeader(cHeader);
        return dataset;
    }

}
